﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CCF20Model
{
    public partial class SMSModel : ModelBase.ModelBaseClass
    {
        public static string BuildManagerReportSMS(string smsBody, string managerName = null)
        {
            string crashTemplate = smsBody;

            if (managerName != null)
            {
                crashTemplate = crashTemplate.Replace("#Zwcw#", managerName);
            }

            
            StatsModel statsModel = new StatsModel();
            IList<DailyStats> dailyStats = statsModel.GetDailyStats();

            //您好：#Zwcw#：，#CompanyName#使用车主无忧驾驶系统的管理数据汇总：
            //截止到今天下午5:30当日注册的中央电脑数量为：#Zc#，
            //当月注册的总数量为:#Dyzc#，
            //当日事故车报警且有人关怀的总数：#Ysg#个，
            //当日事故车报警超过30分钟无人关怀的总的数量为#Wsg#。
            //当日有需要保养且有客户关怀的IDC的数量为#Yby#个
            //有需要保养但超过2小时还没有客户关怀的IDC的数量为#Wby#个
            //当日有故障或者即有故障又有保养的IDC且有客户关怀的IDC数量为#Ygz#个
            //有故障或者即有故障又有保养的IDC但超过1小时没有客户关怀的IDC数量为#Wgz#个
            //当日拒绝回场的客户数量为：#Jjhc#个。')


            foreach (DailyStats stats in dailyStats)
            {
                if (stats.Name1.Contains("当日注册"))
                {
                    crashTemplate = crashTemplate.Replace("#Zc#", stats.Value1.ToString())
                                        .Replace("#Dyzc#", stats.Value2.ToString());
                }
                else if (stats.Name1.Contains("当日事故车"))
                {
                    crashTemplate = crashTemplate.Replace("#Ysg#", stats.Value1.ToString())
                                        .Replace("#Wsg#", stats.Value2.ToString());
                }
                else if (stats.Name1.Contains("当日有需要保养"))
                {
                    crashTemplate = crashTemplate.Replace("#Yby#", stats.Value1.ToString())
                                        .Replace("#Wby#", stats.Value2.ToString());
                }
                else if (stats.Name1.Contains("当日有故障"))
                {
                    crashTemplate = crashTemplate.Replace("#Ygz#", stats.Value1.ToString())
                                        .Replace("#Wgz#", stats.Value2.ToString());
                }
                else if (stats.Name1.Contains("当日拒绝"))
                {
                    crashTemplate = crashTemplate.Replace("#Jjhc#", stats.Value1.ToString());

                }


            }
            return crashTemplate;
        }

        public static string FormatSMS(string crashTemplate, Vehicle vehicle, string customerName = null, bool isFromContact = false)
        {
            string strFault = string.Empty;

            int count =0;
            if (isFromContact)
            {
                foreach (Fault fault in vehicle.Faults.Where(f => f.IsActive == true && f.IsConfirmed == true))
                {
                    fault.IsSMSed = true;
                    strFault = string.Format("{0} {1}-{2},", strFault, fault.Code, fault.CodeDescription);
                    count++;
                }
            }
            else
            {
                foreach (Fault fault in vehicle.Faults.Where(f => f.IsActive == true && f.IsConfirmed == true && f.IsSMSed == false))
                {
                    fault.IsSMSed = true;
                    strFault = string.Format("{0} {1}-{2},", strFault, fault.Code, fault.CodeDescription);
                    count++;
                }
            }
            LoggerHelper.LogCCF20(string.Format("[生成故障短信] 共有{0}条故障 故障短信为 {1}",count,strFault));
            count = 0;
            string strMaintenance = string.Empty;
            if (isFromContact)
            {
                foreach (VehicleMaintenanceHistory history in vehicle.VehicleMaintenanceHistories.Where(h => h.IsActive == true))
                {
                    history.IsSMSed = true;
                    strMaintenance = string.Format("{0},{1}", strMaintenance, history.MaintenanceType.Name);
                    count++;
                }
            }
            else
            {
                foreach (VehicleMaintenanceHistory history in vehicle.VehicleMaintenanceHistories.Where(h => h.IsActive == true && h.IsSMSed == false))
                {
                    history.IsSMSed = true;
                    strMaintenance = string.Format("{0},{1}", strMaintenance, history.MaintenanceType.Name);
                    count++;
                }
            }

            LoggerHelper.LogCCF20(string.Format("[生成保养短信] 共有{0}条保养 保养短信为 {1}", count, strMaintenance));

            string strMonthBegin = string.Format("{0}-{1}-{2}", DateTime.Now.Year, DateTime.Now.Month, 1);
            //DateTime lastDay = DateTime.Today.AddMonths(1).AddDays(-1);
            string strMonthEnd = string.Format("{0}-{1}-{2}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            crashTemplate = crashTemplate.Replace("#lxr#", customerName ?? vehicle.Customer.Name)
                                        .Replace("#NowLs#", vehicle.CurrentMileage.ToString())
                                        .Replace("#oc#", strFault)
                                        .Replace("#ByXmName#", strMaintenance)
                                        .Replace("#IDc#", vehicle.Devices.First().IDCSimNumber)
                                        .Replace("#CarMan#", vehicle.Customer.Name)
                                        .Replace("#CarTel#", vehicle.Customer.FirstMobile)
                                        .Replace("#CarNo#", vehicle.VehicleLicense)
                                        .Replace("#MonthBegin#", strMonthBegin)
                                        .Replace("#MonthEnd#", strMonthEnd);
            return FormatSMS(crashTemplate);


        }

        public static string FormatSMS(string crashTemplate,string managerName = null)
        {
            CompanyProfile profile = ProfileModel.GetCompanyProfile();
            
            if (crashTemplate.Contains("管理数据汇总"))
            {
                
                crashTemplate = BuildManagerReportSMS(crashTemplate,managerName);

            }

            return crashTemplate.Replace("#CompanyName#", profile.CompanyName)
                                .Replace("#CompanyAddress#", profile.CompanyAddress)
                                .Replace("#CompanyPhone#", CurrentUserMobile)
                                .Replace("#ServerPer#", CurrentUser)
                                        ;


        }
    }
}
